package com.example.chinawenddingexpo.repository;

import com.example.chinawenddingexpo.model.Coupon;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface CouponRepository extends JpaRepository<Coupon, Long> {
    
    List<Coupon> findByStatusOrderByCreatedTimeDesc(Coupon.CouponStatus status);
    
    List<Coupon> findByExhibitorIdAndStatusOrderByCreatedTimeDesc(Long exhibitorId, Coupon.CouponStatus status);
    
    @Query("SELECT c FROM Coupon c WHERE c.status = 'ACTIVE' AND c.startTime <= :now AND c.endTime >= :now AND c.usedQuantity < c.totalQuantity")
    List<Coupon> findAvailableCoupons(@Param("now") LocalDateTime now);
    
    @Query("SELECT c FROM Coupon c WHERE c.exhibitor.id = :exhibitorId AND c.status = 'ACTIVE' AND c.startTime <= :now AND c.endTime >= :now")
    List<Coupon> findAvailableCouponsByExhibitor(@Param("exhibitorId") Long exhibitorId, @Param("now") LocalDateTime now);
    
    Coupon findByCouponCode(String couponCode);
}